package com.yanqu.road.dao.impl.activity.cat;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.cat.data.CatDrawNotice;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CatDrawNoticeDaoImpl extends TempDataObjectDao<CatDrawNotice> {

    public static String getCatDrawNoticeSql = "select * from `t_c_activity_cat_draw_notice` where `activityId`=? order by `time` asc limit ?";

    public List<CatDrawNotice> getCatDrawNotice(int activityId, int limit) {
        List<CatDrawNotice> result = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCatDrawNoticeSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                CatDrawNotice data = parse(rs);
                result.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertCatDrawNoticeSql = "insert into `t_c_activity_cat_draw_notice`(activityId,userId,userBaseInfo,type,rewardId,time,reward) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CatDrawNotice data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        if (getDb().executeNoneQuery(insertCatDrawNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCatDrawNoticeSql = "update `t_c_activity_cat_draw_notice` set userId=?,userBaseInfo=?,type=?,rewardId=?,time=? where activityId=?";
    
    @Override
    protected boolean updateData(CatDrawNotice data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateCatDrawNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CatDrawNotice parseData(ResultSet rs) throws SQLException {
        CatDrawNotice catDrawNotice = new CatDrawNotice();
        catDrawNotice.setActivityId(rs.getInt("activityId"));
        catDrawNotice.setUserId(rs.getLong("userId"));
        catDrawNotice.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        catDrawNotice.setType(rs.getInt("type"));
        catDrawNotice.setRewardId(rs.getInt("rewardId"));
        catDrawNotice.setTime(rs.getLong("time"));
        catDrawNotice.setReward(rs.getString("reward"));
        return catDrawNotice;
    }
}
